using System;
using UnityEngine;

[ExecuteInEditMode]
public class ControllerFloorSlope : ControllerGeneric
{
	private static float unit = 0.25f;

	public float length = 10f;

	public float height = 6f;

	public float width = 1f;

	public GameObject prisma1;

	public GameObject prisma2;

	public GameObject cub;

	public GameObject prisma3;

	public GameObject prisma4;

	public float unghiRampa;

	private float widthOld;

	private float heightOld;

	private float lengthOld;

	private bool staticState;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		BumpySurface[] componentsInChildren = base.transform.GetComponentsInChildren<BumpySurface>();
		foreach (BumpySurface bumpySurface in componentsInChildren)
		{
			bumpySurface.FreezeBumps();
		}
		SetNames();
		SetParenting();
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		else
		{
			UnityEngine.Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (Application.isEditor && !Application.isPlaying)
		{
			SetupRemoteControl();
			if (IsRemoteControlActive())
			{
				base.transform.position = remoteControl.transform.position;
				base.transform.rotation = remoteControl.transform.rotation;
				length = remoteControl.transform.localScale.z;
				height = remoteControl.transform.localScale.y;
				width = remoteControl.transform.localScale.x;
			}
			if (length < ControllerGeneric.floorHeight)
			{
				length = ControllerGeneric.floorHeight;
			}
			if (height < 2f * ControllerGeneric.floorHeight)
			{
				height = 2f * ControllerGeneric.floorHeight;
			}
			if (width < 0.0001f)
			{
				width = 0.0001f;
			}
			if (remoteControl != null)
			{
				remoteControl.transform.localScale = new Vector3(width, height, length);
			}
			Setup();
			SetupUVs();
		}
	}

	private void Setup()
	{
		AssureParenting();
		SetNames();
		if (needsRefresh || widthOld != width || heightOld != height || lengthOld != length)
		{
			needsRefresh = false;
			widthOld = width;
			heightOld = height;
			lengthOld = length;
			Vector3 zero = Vector3.zero;
			Vector3 one = Vector3.one;
			Vector3 zero2 = Vector3.zero;
			float num = Mathf.Sqrt(length * length + (height - 2f * ControllerGeneric.floorHeight) * (height - 2f * ControllerGeneric.floorHeight));
			float num2 = Mathf.Sqrt(num * num - ControllerGeneric.floorHeight * ControllerGeneric.floorHeight);
			float num3 = Mathf.Acos(length / num) * 57.29578f;
			float num4 = Mathf.Acos(num2 / num) * 57.29578f;
			float num5 = num3 + num4;
			zero = new Vector3(0f, height * 0.5f, length * 0.5f);
			one.z = num2 / unit;
			one.y = ControllerGeneric.floorHeight / unit;
			one.x = width / unit;
			zero2.x = 0f - num5;
			float num6 = (height - ControllerGeneric.floorHeight) / Mathf.Sin(num5 * ((float)Math.PI / 180f));
			float num7 = num6 - num2;
			float num8 = num7 * Mathf.Sin(num5 * ((float)Math.PI / 180f));
			float num9 = ControllerGeneric.floorHeight * Mathf.Cos((90f - num5) * ((float)Math.PI / 180f));
			Vector3 one2 = Vector3.one;
			one2.y = num7 / unit;
			one2.z = ControllerGeneric.floorHeight / unit;
			one2.x = width / unit;
			if (cub != null)
			{
				cub.transform.localPosition = zero - new Vector3(0f, height / 2f, length / 2f);
				cub.transform.localEulerAngles = zero2;
				cub.transform.localScale = one;
			}
			if (prisma1 != null)
			{
				prisma1.transform.localPosition = Vector3.zero - new Vector3(0f, height / 2f, length / 2f);
				prisma1.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
				prisma1.transform.localScale = one2;
			}
			if (prisma2 != null)
			{
				prisma2.transform.localPosition = new Vector3(0f, num8, num9) - new Vector3(0f, height / 2f, length / 2f);
				prisma2.transform.localEulerAngles = new Vector3(90f + num5, 180f, 0f);
				prisma2.transform.localScale = one2;
			}
			if (prisma3 != null)
			{
				prisma3.transform.localPosition = new Vector3(0f, height - num8, length - num9) - new Vector3(0f, height / 2f, length / 2f);
				prisma3.transform.localEulerAngles = new Vector3(-90f + num5, 180f, 0f);
				prisma3.transform.localScale = one2;
			}
			if (prisma4 != null)
			{
				prisma4.transform.localPosition = new Vector3(0f, height, length) - new Vector3(0f, height / 2f, length / 2f);
				prisma4.transform.localEulerAngles = new Vector3(-90f, 0f, 0f);
				prisma4.transform.localScale = one2;
			}
		}
	}

	private void SetStatic(bool newStaticState = true)
	{
		if (newStaticState != staticState)
		{
			staticState = newStaticState;
			cub.isStatic = staticState;
			prisma1.isStatic = staticState;
			prisma2.isStatic = staticState;
			prisma3.isStatic = staticState;
			prisma4.isStatic = staticState;
		}
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
	}

	private void SetNames()
	{
		cub.name = "FloorSlope";
		prisma1.name = "FloorSlopePrism1";
		prisma2.name = "FloorSlopePrism2";
		prisma3.name = "FloorSlopePrism3";
		prisma4.name = "FloorSlopePrism4";
		if (base.gameObject.tag == "slippery")
		{
			cub.name += "Slippery";
			prisma1.name += "Slippery";
			prisma2.name += "Slippery";
			prisma3.name += "Slippery";
			prisma4.name += "Slippery";
		}
	}

	private void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, length);
			remoteControl.gameObject.AddComponent<PresetsFloorSlope>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
		ControllerFloorSlope component = rc.controller.GetComponent<ControllerFloorSlope>();
		unghiRampa = component.unghiRampa;
		PresetsFloorSlope component2 = rc.gameObject.GetComponent<PresetsFloorSlope>();
		PresetsFloorSlope component3 = remoteControl.GetComponent<PresetsFloorSlope>();
		component3.lockHeight = component2.lockHeight;
		component3.lockLenght = component2.lockLenght;
		component3.angle = component2.angle;
	}

	public void SetupUVs()
	{
		Mesh mesh = prisma1.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		float num = 1f / 32f;
		float num2 = 1f;
		uv[17].x = 0.5f - prisma1.transform.localScale.y * num;
		uv[17].y = 1f * num2;
		uv[18].x = 0.5f + prisma1.transform.localScale.y * num;
		uv[18].y = 0f * num2;
		uv[19].x = 0.5f - prisma1.transform.localScale.y * num;
		uv[19].y = 0f * num2;
		uv[14].x = 0.5f - prisma1.transform.localScale.y * num;
		uv[14].y = 0f * num2;
		uv[15].x = 0.5f + prisma1.transform.localScale.y * num;
		uv[15].y = 0f * num2;
		uv[16].x = 0.5f - prisma1.transform.localScale.y * num;
		uv[16].y = 1f * num2;
		num = 0.0625f;
		num2 = 0.25f;
		uv[0].x = 0.5f - prisma1.transform.localScale.x * num;
		uv[0].y = 0.5f + prisma1.transform.localScale.z * num2;
		uv[1].x = 0.5f + prisma1.transform.localScale.x * num;
		uv[1].y = 0.5f - prisma1.transform.localScale.z * num2;
		uv[2].x = 0.5f - prisma1.transform.localScale.x * num;
		uv[2].y = 0.5f - prisma1.transform.localScale.z * num2;
		uv[3].x = 0.5f + prisma1.transform.localScale.x * num;
		uv[3].y = 0.5f + prisma1.transform.localScale.z * num2;
		mesh.uv = uv;
		mesh = prisma2.GetComponent<MeshFilter>().mesh;
		uv = mesh.uv;
		num = 1f / 32f;
		num2 = 1f;
		uv[14].x = 0.5f - prisma2.transform.localScale.y * num;
		uv[14].y = 0f * num2;
		uv[15].x = 0.5f + prisma2.transform.localScale.y * num;
		uv[15].y = 0f * num2;
		uv[16].x = 0.5f - prisma2.transform.localScale.y * num;
		uv[16].y = 1f * num2;
		uv[17].x = 0.5f - prisma2.transform.localScale.y * num;
		uv[17].y = 1f * num2;
		uv[18].x = 0.5f + prisma2.transform.localScale.y * num;
		uv[18].y = 0f * num2;
		uv[19].x = 0.5f - prisma2.transform.localScale.y * num;
		uv[19].y = 0f * num2;
		mesh.uv = uv;
		mesh = prisma3.GetComponent<MeshFilter>().mesh;
		uv = mesh.uv;
		uv[17].x = 0.5f - prisma3.transform.localScale.y * num;
		uv[17].y = 0f * num2;
		uv[18].x = 0.5f + prisma3.transform.localScale.y * num;
		uv[18].y = 1f * num2;
		uv[19].x = 0.5f - prisma3.transform.localScale.y * num;
		uv[19].y = 1f * num2;
		uv[14].x = 0.5f - prisma3.transform.localScale.y * num;
		uv[14].y = 1f * num2;
		uv[15].x = 0.5f + prisma3.transform.localScale.y * num;
		uv[15].y = 1f * num2;
		uv[16].x = 0.5f - prisma3.transform.localScale.y * num;
		uv[16].y = 0f * num2;
		num = 1f / 32f;
		num2 = 0.0625f;
		float num3 = 0.5f - cub.transform.localScale.z * 1f / 32f;
		uv[8].x = 0.5f - prisma3.transform.localScale.x * num;
		uv[8].y = num3 - prisma3.transform.localScale.y * num2;
		uv[9].x = 0.5f + prisma3.transform.localScale.x * num;
		uv[9].y = num3;
		uv[10].x = 0.5f + prisma3.transform.localScale.x * num;
		uv[10].y = num3 - prisma3.transform.localScale.y * num2;
		uv[11].x = 0.5f + prisma3.transform.localScale.x * num;
		uv[11].y = num3;
		uv[12].x = 0.5f - prisma3.transform.localScale.x * num;
		uv[12].y = num3 - prisma3.transform.localScale.y * num2;
		uv[13].x = 0.5f - prisma3.transform.localScale.x * num;
		uv[13].y = num3;
		mesh.uv = uv;
		mesh = prisma4.GetComponent<MeshFilter>().mesh;
		uv = mesh.uv;
		num = 1f / 32f;
		num2 = 1f;
		uv[14].x = 0.5f - prisma4.transform.localScale.y * num;
		uv[14].y = 1f * num2;
		uv[15].x = 0.5f + prisma4.transform.localScale.y * num;
		uv[15].y = 1f * num2;
		uv[16].x = 0.5f - prisma4.transform.localScale.y * num;
		uv[16].y = 0f * num2;
		uv[17].x = 0.5f - prisma4.transform.localScale.y * num;
		uv[17].y = 0f * num2;
		uv[18].x = 0.5f + prisma4.transform.localScale.y * num;
		uv[18].y = 1f * num2;
		uv[19].x = 0.5f - prisma4.transform.localScale.y * num;
		uv[19].y = 1f * num2;
		num = 1f / 32f;
		num2 = 0.0625f;
		num3 = 0.5f - cub.transform.localScale.z * 1f / 32f - prisma3.transform.localScale.y * num2;
		uv[8].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[8].y = num3;
		uv[9].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[9].y = num3 - prisma4.transform.localScale.y * num2;
		uv[10].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[10].y = num3;
		uv[11].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[11].y = num3 - prisma4.transform.localScale.y * num2;
		uv[12].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[12].y = num3;
		uv[13].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[13].y = num3 - prisma4.transform.localScale.y * num2;
		num = 0.0625f;
		num2 = 0.25f;
		uv[0].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[0].y = 0.5f - prisma4.transform.localScale.z * num2;
		uv[1].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[1].y = 0.5f + prisma4.transform.localScale.z * num2;
		uv[2].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[2].y = 0.5f + prisma4.transform.localScale.z * num2;
		uv[3].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[3].y = 0.5f - prisma4.transform.localScale.z * num2;
		mesh.uv = uv;
		mesh = cub.GetComponent<MeshFilter>().mesh;
		uv = mesh.uv;
		num = 1f / 32f;
		num2 = 1f / 32f;
		uv[4].x = 0.5f + cub.transform.localScale.x * num;
		uv[6].x = 0.5f - cub.transform.localScale.x * num;
		uv[5].x = 0.5f - cub.transform.localScale.x * num;
		uv[7].x = 0.5f + cub.transform.localScale.x * num;
		uv[4].y = 0.5f + cub.transform.localScale.z * num2;
		uv[6].y = 0.5f + cub.transform.localScale.z * num2;
		uv[5].y = 0.5f - cub.transform.localScale.z * num2;
		uv[7].y = 0.5f - cub.transform.localScale.z * num2;
		num = 0.0625f;
		num2 = 0.5f;
		uv[16].x = 0.5f + cub.transform.localScale.z * num;
		uv[18].x = 0.5f - cub.transform.localScale.z * num;
		uv[17].x = 0.5f - cub.transform.localScale.z * num;
		uv[19].x = 0.5f + cub.transform.localScale.z * num;
		uv[16].y = 0.5f + num2;
		uv[18].y = 0.5f + num2;
		uv[17].y = 0.5f - num2;
		uv[19].y = 0.5f - num2;
		uv[20].x = 0.5f - cub.transform.localScale.z * num;
		uv[22].x = 0.5f + cub.transform.localScale.z * num;
		uv[21].x = 0.5f + cub.transform.localScale.z * num;
		uv[23].x = 0.5f - cub.transform.localScale.z * num;
		uv[20].y = 0.5f + num2;
		uv[22].y = 0.5f + num2;
		uv[21].y = 0.5f - num2;
		uv[23].y = 0.5f - num2;
		mesh.uv = uv;
	}
}
